<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2021/6/14
 * Time: 12:24
 */

namespace App\Http\Controllers\api\common;


use App\Http\Controllers\api\base\BaseController;
use App\Http\Resources\news\CollectResource;
use App\Models\config\Config;
use App\Models\news\Comment;
use App\Models\User;
use App\Models\user\Collect;
use App\Models\user\MoneyDetail;
use App\Models\user\SportLog;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CommonController extends BaseController
{

    /**
     * 签到
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function sign(Request $request)
    {

        $signDayCount = MoneyDetail::where('user_id','=',$this->user_id)
            ->where('created_at','>=',Carbon::today())
            ->where('title','=','签到')
            ->get()
            ->count();
        $data['sign_status'] = 1;
        $data['run_days'] = $this->user->run_days;
        if($signDayCount > 0){
            return success($data);
        }
        DB::beginTransaction();
        try {
            $amount = Config::getConfig('sign_score')['value'];
            $moneyLog = MoneyDetail::create([
                'user_id' => $this->user_id,
                'amount' => $amount,
                'title' => '签到',
                'pm' => 1
            ]);
            $money = bcadd($this->user->money , $amount, 2);
            $this->user->money = $money;
            $yesterdayCount = MoneyDetail::where('user_id','=',$this->user_id)
                ->where('created_at','>=',Carbon::yesterday())
                ->where('title','=','签到')
                ->get()
                ->count();
            if($yesterdayCount == 0){
                $this->user->run_days = 1;
            }else{
                $this->user->run_days += 1;
            }
            $user = $this->user->save();
            if(!$moneyLog || !$user){
                DB::rollBack();
                $data['sign_status'] = 0;
                return success($data);
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $data['sign_status'] = 0;
            return error($e->getMessage(),$data);
        }
        return success($data,'签到成功');
    }


    /**
     * 获取积分
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function getScore(Request $request)
    {
        $type = $request->type;
        switch ($type){
            case 1 :
                $amount = Config::getConfig('read_news_score')['value'];
                $insertData = [
                    'user_id' => $this->user_id,
                    'amount' => $amount,
                    'title' => '看文章',
                    'pm' => 1,
                    'resource_id' => $request->resource_id
                ];break;
            case 2 :
                $amount = Config::getConfig('read_video_score')['value'];
                $insertData = [
                    'user_id' => $this->user_id,
                    'amount' => $amount,
                    'title' => '看视频',
                    'pm' => 1,
                    'resource_id' => $request->resource_id
                ];break;
            case 3 :
                $amount = Config::getConfig('read_exam_score')['value'];
                $insertData = [
                    'user_id' => $this->user_id,
                    'amount' => $amount,
                    'title' => '每日答题',
                    'pm' => 1,
                    'resource_id' => $request->resource_id
                ];
        }
        DB::beginTransaction();
        try{
            $moneyLog = MoneyDetail::create($insertData);
            $money = bcadd($this->user->money , $amount, 2);
            $user = User::where(['id'=>$this->user_id])->update(['money'=>$money]);
            if(!$moneyLog || !$user){
                DB::rollBack();
                return error('签到失败');
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return error($e->getMessage());
        }
        return success(['score'=>$money]);
    }

    /**
     * 获取积分规则
     * @return mixed
     */
    public function getScoreRule()
    {
        $data['get_rule'] = Config::getConfig('score_get_rule_text');
        $data['exchange_rule'] = Config::getConfig('score_exchange_rule_text');
        return success($data);
    }
    
    /**
     * 运动步数上传
     * @param Request $request
     * @return mixed
     */
    public function sportRecord(Request $request)
    {
        $number = $request->number;
        $data = [
            'user_id'   => $this->user_id,
            'number'    => $number
        ];
        $data = SportLog::add($data);
        return success($data);
    }

    /**
     * 当日运动计数排行
     * @return mixed
     */
    public function sportRank()
    {
        $data = SportLog::rankList();
        foreach ($data as &$item){
            $item['user_name'] = $item['user']['name'];
            $item['header_img'] = $item['user']['header_img'];
            unset($item['user']);
        }
        return success($data);
    }

    /**
     * 收藏列表
     * @param Request $request
     * @return mixed
     */
    public function collectList(Request $request)
    {
        $type = $request->type;
        $user_id = $this->user_id;
        if(!is_numeric($type)){
            return error(205);
        }
        $pageSize = $request->page_size;
        if(!$pageSize || !is_numeric($pageSize)){
            $pageSize = 10;
        }
        $data = Collect::collectList($user_id,$type,$pageSize);
        foreach ($data as &$item){
            $item['comment_number'] = 0;
            if($item['type'] == 1){
                $item['comment_number'] = Comment::where('news_id',$item['resource_id'])->count();
            }
        }
        return success(new CollectResource($data));
    }

    public function collect(Request $request)
    {
        $user_id = $this->user_id;
        $type = $request->type;
        $resource_id = $request->resource_id;
        $data = Collect::collect($type,$user_id,$resource_id);
        return success($data);
    }

    public function about()
    {
        $data['conent'] = "关于我们";
        return success($data);
    }

    /**
     * 考核公告
     * @return mixed
     */
    public function accessNotice()
    {
        $data = Config::where('name','access_vote_notice')
            ->select(['value as content'])->first();
        return success($data);
    }

}